package com.goodcol.plugin.tool;

import com.goodcol.plugin.dto.GenerateOptions;
import com.goodcol.plugin.entity.TableInfo;
import com.goodcol.plugin.entity.Template;
import com.goodcol.plugin.entity.TemplateGroup;
import com.goodcol.plugin.service.impl.CodeGenerateServiceImpl;

import java.util.Collections;
import java.util.Map;
import java.util.Objects;

/**
 * 额外的代码生成工具
 *
 * @author QuickCode
 * @version 1.0.0
 */
public class ExtraCodeGenerateUtils {
    /**
     * 代码生成服务
     */
    private CodeGenerateServiceImpl codeGenerateService;
    /**
     * 表信息对象
     */
    private TableInfo tableInfo;
    /**
     * 生成配置
     */
    private GenerateOptions generateOptions;

    public ExtraCodeGenerateUtils(CodeGenerateServiceImpl codeGenerateService, TableInfo tableInfo, GenerateOptions generateOptions) {
        this.codeGenerateService = codeGenerateService;
        this.tableInfo = tableInfo;
        this.generateOptions = generateOptions;
    }

    /**
     * 生成代码
     *
     * @param templateGroupName 模板组名称
     * @param templateName      模板名称
     * @param param             附加参数
     */
    public void run(String templateGroupName, String templateName, Map<String, Object> param) {
        // 获取到模板
        TemplateGroup templateGroup = SettingGroupUtils.getTemplateByGroup(templateGroupName);
        for (Template template : templateGroup.getElementList()) {
            if (Objects.equals(template.getName(), templateName)) {
                // 生成代码
                codeGenerateService.generate(Collections.singletonList(template),
                        SettingGroupUtils.getGlobalConfigByGroup(templateGroup.getGlobalConfigName()),
                        Collections.singletonList(this.tableInfo), this.generateOptions, param);
                break;
            }
        }
    }
}
